<?php

namespace app\proxy\controller;

use app\data\model\LiveAppList;
use app\data\model\LiveIntegarlThali;
use app\data\model\LiveIntegralRecord;
use app\data\model\LiveProxyList;
use app\data\model\LiveIntegralOrder;
use think\admin\Controller;
use think\admin\model\SystemUser;
use think\Session;

/**
 * 积分订单
 * Class IntegralOrderController
 * @package app\proxy\controller
 */
class IntegralOrderController extends Controller
{
    /**
     * 查询
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function index()
    {
        $this->title = '积分订单';
        $this->sysusers = SystemUser::mk()->field('id,nickname')->select()->toArray();
        $this->apps = LiveAppList::mk()->field('app_id,app_name')->select()->toArray();
        $this->proxys = LiveProxyList::mk()->field('id,user_account,user_nickname')->select()->toArray();
        $this->intethalis = LiveIntegarlThali::mk()->field('inte_id,inte_thali_name')->where(['deleted' => 0])->select()->toArray();

        // 状态数据统计
        $this->total = ['t1' => 0, 't2' => 0, 't3' => 0];
        foreach (LiveIntegralOrder::mk()->field('order_status,count(order_status) as total')->group('order_status')->cursor() as $vo) {
            [$this->total["t{$vo['order_status']}"] = $vo['total']];
        }
        $query = LiveIntegralOrder::mQuery();
        // 列表选项卡
        if (is_numeric($this->type = trim(input('type', 't1'), 't'))) {
            $query->where(['order_status' => $this->type]);
        }
        $query->equal('app_id,proxy_id');
        $query->dateBetween('create_time');
        $query->order('create_time desc')->page();
    }
    /**
     * 审核
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function examine(){
        $post =  $this->request->post();
        if(!isset($post['id'])) $this->error('不存在ID');
        $order = LiveIntegralOrder::mk()->where(['id'=>$post['id']])->find();
        if($order['examine_status'] == 1) $this->error('积分订单已审核！','javascript:location.reload()');
        if($order['order_status'] != 1) $this->error('订单已支付或已取消！','javascript:location.reload()');

        //修改为 已支付  已审核
        LiveIntegralOrder::mk()->where(['id'=>$post['id']])->update([
            'examine_status'        =>1,//审核状态 0未审核 1审核通过
            'examine_userid'        =>Session('user')['id'],
            'examine_time'          =>date('Y-m-d H:i:s'),
            'order_status'          =>2,//订单状态(1预订单,2已支付,3已取消)
        ]);
        //充值积分
        $inteThali= LiveIntegarlThali::mk()->where(['inte_id'=>$order['inte_thaliid']])->find();
        $add_inte_num = $inteThali['inte_num'];
        $proxy = LiveProxyList::mk()->where(['id'=>$order['proxy_id']])->find();
        $new_integral_balance = $proxy['integral_balance'] + $add_inte_num;
        LiveProxyList::mk()->where(['id'=>$order['proxy_id']])->update(['integral_balance'=>$new_integral_balance]);
        //积分记录
        LiveIntegralRecord::mk()->insert([
            'uuid'              =>$order['proxy_id'],
            'app_id'            =>$order['app_id'],
            'integral_type'     =>1,//积分类型 1收入 2支出
            'integral_num'      =>$add_inte_num,
            'integral_balance'  =>$new_integral_balance,
            'create_uuid'       =>$order['proxy_id'],
            'remarks'=>'购买积分套餐['.$inteThali['inte_thali_name'].']，充值['.$add_inte_num.']积分'
        ]);
        $this->success('充值成功');
    }
}